﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Utils;

namespace LinkCard.CustomerView.CusCp
{
    public partial class point_transfer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.PointH.Value = GetPointInfo().ToString();
            this.Point.Text = GetPointInfo().ToString();
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            try
            {
                var db = new TopLinkEntities();

                Int64 nCustomerID = Convert.ToInt64(Session["USERID"].ToString());
                var CardNumber = this.sCardNumber.Value;
                var _customer = db.Customers.Where(t => t.CustomerID == nCustomerID).FirstOrDefault();

                var _cusReceiver = db.Customers.Where(t => t.CardNumber == CardNumber).FirstOrDefault();

                var logTransfer = new Model.TransferPoint();
                //Bước 1: Trừ điểm
                //Bước 2: Cộng điểm
                //Bước 3: Ghi log
                //Bước 4: Nhắn SMS

                var nTransferPoint = this.TransferedPoint.Value.Trim();
                var nFee = this.TransferFee.Value;
                var nRemainPoint = this.RemainPoint.Value;

                _customer.Point = ConvertUtility.ToInt32(nRemainPoint); //trừ điểm

                _cusReceiver.Point = _cusReceiver.Point + ConvertUtility.ToInt32(nTransferPoint); //cộng điểm

                //Ghi log
                logTransfer.DateLog = DateTime.Now;
                logTransfer.Fee = ConvertUtility.ToInt32(nFee);
                logTransfer.FromID = _cusReceiver.CustomerID;
                logTransfer.ToID = _cusReceiver.CustomerID;
                logTransfer.TransferPoint1 = ConvertUtility.ToInt32(nTransferPoint);

                db.AddToTransferPoint(logTransfer);

                db.SaveChanges();
                
                //nhắn tin
                //Gửi tin nhắn
                if ((_customer.MobilePhone != "") && _customer.MobilePhone.Length >= 10)
                {
                    Int64 _kqSMS = Utils.SMSTool.SendSMS(_customer.MobilePhone, "Ban da chuyen so diem: " + nTransferPoint + " den " + _cusReceiver.Fullname + " thanh cong.");
                    Int64 _kqSMS1 = Utils.SMSTool.SendSMS(_cusReceiver.MobilePhone, "Ban da nhan duoc so diem: " + nTransferPoint + " tu " + _customer.Fullname + ".");
                    if (_kqSMS < 0) //nếu nhắn tin lỗi thì log lại lỗi, nhắn ok thì thôi ko lưu lại nữa
                    {
                        
                    }
                }
                Response.Redirect("message_box.aspx?msg=1", true);

            }
            catch
            {
 
            }
        }

        //Lấy thông tin về điểm của Customer
        [System.Web.Services.WebMethod]
        public static string GetCustomerInfo(string sCardNumber)
        {
            try
            {
                //var iCardNumber = Convert.ToInt64(sCardNumber);
                TopLinkEntities db = new TopLinkEntities();
                var customerInfo = db.Customers.Where(t => t.CardNumber == sCardNumber).FirstOrDefault();

                if (customerInfo != null)
                {
                    if (customerInfo.Status == 2)
                        return customerInfo.Fullname.ToString();
                    else
                        return "-1"; //Tai khoan chua duoc active
                }
                else
                    return "-2"; //Khong co tai khoan
            }
            catch
            {
                return "-3"; //Xay ra loi
            }
        }

        //Lấy thông tin người Tặng điểm
        public int GetPointInfo()
        {
            try
            {
                Int64 nCustomerID = Convert.ToInt64(Session["USERID"].ToString());
                var db = new TopLinkEntities();
                var _customer = db.Customers.Where(t => t.CustomerID == nCustomerID).FirstOrDefault();

                if (_customer != null)
                    return ConvertUtility.ToInt32(_customer.Point);
                else
                    return 0;
            }
            catch
            {
                return -1;
            }

        }
    }
}